package com.yima.baseproj.module.app.dict.dict;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yima.baseproj.module.app.dict.data.AppDictDataEntity;
import com.yima.baseproj.module.app.dict.data.AppDictDataService;
import com.yima.baseproj.module.app.dict.dict.model.AppDictAddParamModel;
import com.yima.baseproj.module.app.dict.dict.model.AppDictEditParamModel;
import com.yima.baseproj.module.app.dict.dict.model.AppDictSearchModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AppDictService extends ServiceImpl<AppDictMapper, AppDictEntity> {

    @Resource
    public AppDictDataService appDictDataService;

    public Page<Map> findByPage(Page page, AppDictSearchModel searchVo) {
        return baseMapper.findBySearchVo(page,searchVo);
    }

    public List<Map> findByList(AppDictSearchModel searchVo) {
        return baseMapper.findBySearchVo(searchVo);
    }

    /**
     * 添加 - 事务
     * @param paramModel
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(AppDictAddParamModel paramModel) {

    }

    @Transactional(rollbackFor = Exception.class)
    public void delByIds(List<String> idList) {
        appDictDataService.remove(Wrappers.<AppDictDataEntity>query().in("dict_id",idList));
        baseMapper.deleteBatchIds(idList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void edit(AppDictEditParamModel paramModel) {

    }

    public AppDictEntity findByType(String type) {
        return getOne(Wrappers.<AppDictEntity>query().eq("type",type));
    }

    public AppDictEntity findByTypeAndStateIsTrue(String type) {
        return getOne(Wrappers.<AppDictEntity>query().eq("type",type).eq("state",true));
    }

    public List<AppDictEntity> findAllByStateIsTrue() {
        return list(Wrappers.<AppDictEntity>query().eq("state",true));
    }
}